package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.SkuDetailFeignClient;
import com.atguigu.gmall.item.to.SkuDetailTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author:lyd
 * @Date:2023/8/18 19:50
 * @Version:1.0
 * @Description
 **/

@Controller
public class ItemController {

    @Autowired
    private SkuDetailFeignClient skuDetailFeignClient;

    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId, Model model) {
        Result<SkuDetailTO> skuDetail = skuDetailFeignClient.getSkuDetail(skuId);
        if (skuDetail.getCode() != 200) {
            return "item/404";
        }
        SkuDetailTO data = skuDetail.getData();
        if (data!= null) {
            model.addAttribute("categoryView", data.getCategoryView());
            model.addAttribute("skuInfo", data.getSkuInfo());
            model.addAttribute("price", data.getPrice());
            model.addAttribute("spuSaleAttrList", data.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson", data.getValuesSkuJson());
            return "item/index";
        }
        return "item/404";

    }
}
